package com.nuoniu.sibanyun.controller.crm;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmSecretKey;
import com.nuoniu.sibanyun.service.crm.ICrmSecretKeyService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 秘钥管理 前端控制器
 * </p>
 *
 * @author baiqiang
 * @since 2020-07-17
 */
@RestController
@RequestMapping("/member/secretKey")
@Api(tags = "秘钥管理")
public class CrmSecretKeyController extends BaseController {
    
    @Resource
    private ICrmSecretKeyService secretKeyService;

    /**
     * 获取密钥列表
     * @author 小啊刚
     * @date 2020-07-17
     */
    @GetMapping("page")
    public HttpResult pageGoods(Page page, CrmSecretKey secretKey) {
        return HttpResult.ok(secretKeyService.pageGoods(page, secretKey));
    }

    /**
     * 保存密钥
     * @author 小啊刚
     * @date 2020-07-17
     */
    @PostMapping("save")
    public HttpResult saveGoods(@RequestBody CrmSecretKey secretKey) {
        for(int i=1; i<= secretKey.getCount(); i++) {
            String key = secretKeyService.getBySecretKey();
            secretKey.setSecretKey(key);
            secretKeyService.save(secretKey);
        }
        return HttpResult.ok();
    }

    /**
     * 停用密钥
     * @author 小啊刚
     * @date 2020-07-17
     */
    @PostMapping("disable")
    public HttpResult disable(@RequestBody List<Long> ids) {
        for(Long id : ids) {
            CrmSecretKey secretKey = new CrmSecretKey();
            secretKey.setId(id);
            secretKey.setStatus(2);
            secretKeyService.updateById(secretKey);
        }
        return HttpResult.ok();
    }

    /**
     * 启用密钥
     * @author 小啊刚
     * @date 2020-07-17
     */
    @PostMapping("enable")
    public HttpResult enable(@RequestBody List<Long> ids) {
        for(Long id : ids) {
            CrmSecretKey secretKey = new CrmSecretKey();
            secretKey.setId(id);
            secretKey.setStatus(0);
            secretKeyService.updateById(secretKey);
        }
        return HttpResult.ok();
    }
}
